﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommerceOne.Domains.Entities;
using CommerceOne.Domains.Repositories;
using CommerceOne.Domains.SqlRepositories;
using CommerceOne.Models;
using CommerceOne.Widgets.PagingAndSortingNav;

namespace CommerceOne.Controllers
{
    public class ProductController : ControllerBase
    {
        private readonly IProductRepository _productRepository = new SqlProductRepository();
        private ICommentRepository _commentRepository = new SqlCommentRepository();

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Details the specified product id.
        /// </summary>
        /// <param name="objectId">The object id.</param>
        /// <returns></returns>
        public ActionResult Detail(int objectId)
        {
            ProductViewModel viewModel = new ProductViewModel();
            this.CommonInitialize(viewModel);

            viewModel.Product = this._productRepository.GetProduct(objectId);
            ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
            productCommentViewModel.ProductId = viewModel.Product.Id;

            int pageSize = 12;
            int page = 1;
            int total = this._commentRepository.Total(objectId);

            PagingSortingNavController pagingController = new PagingSortingNavController();
            productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
            productCommentViewModel.Comments = this._commentRepository.GetComment(objectId, page, pageSize);
            viewModel.ProductComment = productCommentViewModel;

            return View(viewModel);
        }

        /// <summary>
        /// Details the specified product id.
        /// </summary>
        /// <param name="objectId">The object id.</param>
        /// <param name="contents">The contents.</param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public PartialViewResult Detail(long objectId, string contents)
        {
            Comment comment = new Comment();
            comment.ProductId = objectId;
            comment.Content = contents;
            comment.UserId = 5;
            comment.Title = "default title";
            comment.Date = DateTime.Now;
            this._commentRepository.AddComment(comment);

            ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
            productCommentViewModel.ProductId = comment.ProductId;

            int pageSize = 12;
            int page = 1;
            int total = this._commentRepository.Total(comment.ProductId);

            PagingSortingNavController pagingController = new PagingSortingNavController();
            productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
            productCommentViewModel.Comments = this._commentRepository.GetComment(comment.ProductId, page, pageSize);

            return PartialView("_Comments", productCommentViewModel);
        }

        public PartialViewResult Comments(long objectId, int page)
        {
            ProductCommentViewModel productCommentViewModel = new ProductCommentViewModel();
            productCommentViewModel.ProductId = objectId;

            int pageSize = 12;
            int total = this._commentRepository.Total(objectId);

            PagingSortingNavController pagingController = new PagingSortingNavController();
            productCommentViewModel.PagingSortingViewModel = pagingController.GetViewModel(total, pageSize, page, "/1/Product/{0}");
            productCommentViewModel.Comments = this._commentRepository.GetComment(objectId, page, pageSize);

            return PartialView("_Comments", productCommentViewModel);
        }
    }
} 
